package com.zxy.supplier_system.server.repository

import com.zxy.supplier_system.server.entity.Employee
import com.zxy.supplier_system.server.entity.SystemService
import com.zxy.supplier_system.server.utils.joinOnce
import jakarta.persistence.criteria.From
import jakarta.persistence.criteria.Root
import org.springframework.data.jpa.domain.Specification
import org.springframework.data.jpa.repository.JpaRepository

interface SystemServiceRepository : JpaRepository<SystemService, String> {
    object Specifications {

        fun <T, A : Any, B : Any> systemServiceEquals(
            parentPathBuilder: (root: Root<T>) -> From<A, B>,
            systemService: SystemService
        ): Specification<T> {
            return Specification<T> { root, _, cb ->
                val parentPath = parentPathBuilder(root)
                val systemServiceJoin = parentPath.joinOnce<B, Employee>("systemService")
                cb.equal(systemServiceJoin.get<String>("id"), systemService.id)
            }
        }

    }

}
